/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yami.shop.bean.app.dto.UserLabelDto;
import com.yami.shop.bean.app.param.UserLabelParam;
import com.yami.shop.bean.model.Area;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserLabel;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.AreaService;
import com.yami.shop.service.UserLabelService;
import com.yami.shop.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lgh on 2018/10/26.
 */
@RestController
@RequestMapping("/p/userLabel")
@Api(tags = "用户标签接口")
public class UserLabelController {

    @Autowired
    private UserLabelService userLabelService;
    @Autowired
    private MapperFacade mapperFacade;
    @Autowired
    private UserService userService;

    @GetMapping
    @ApiOperation(value = "分页获取标签", notes = "分页获取标签")
    public ResponseEntity<IPage<UserLabel>> list(PageParam<UserLabel> page) {
        Page<UserLabel> userLabelPage = userLabelService.page(page, Wrappers.lambdaQuery(new UserLabel())
                .eq(UserLabel::getStatus, 2));
        return ResponseEntity.ok(userLabelPage);
    }

    @PutMapping("/create")
    @ApiOperation(value = "创建标签", notes = "创建标签")
    public ResponseEntity<Void> add(@RequestBody UserLabel userLabel) {
        userLabel.setId(IdUtil.simpleUUID());
        userLabel.setCreateTime(new Date());
        userLabel.setStatus(1);
        userLabelService.save(userLabel);
        return ResponseEntity.ok(null);
    }

    @PutMapping("/add")
    @ApiOperation(value = "给用户添加标签", notes = "给用户添加标签")
    public ResponseEntity<Void> addToUser(@ApiParam("用户标签名称 逗号连接") @RequestBody String userLabels) {
        String userId = SecurityUtils.getUser().getUserId();
        User user = new User();
        user.setUserId(userId);
        user.setUserLabels(userLabels);
        userService.updateById(user);
        return ResponseEntity.ok(null);
    }

}
